package com.doctorliang.cheng.shopping.home.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.doctorliang.cheng.shopping.R;
import com.doctorliang.cheng.shopping.home.bean.ResultBeanData;
import com.doctorliang.cheng.shopping.utils.Constants;

import java.util.List;

/**
 * Created by CHENG on 2017/9/28.
 */

public class SeckillRecycleViewAdapter extends RecyclerView.Adapter {
    private final Context mcontext;
    private final List<ResultBeanData.ResultBean.SeckillInfoBean.ListBean> list;

    public SeckillRecycleViewAdapter(Context mcontext, List<ResultBeanData.ResultBean.SeckillInfoBean.ListBean> list) {
        this.mcontext = mcontext;
        this.list = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(mcontext).inflate(R.layout.item_skill1, null));

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        MyViewHolder myViewHolder = (MyViewHolder) holder;
        //根据位置得到数据
        ResultBeanData.ResultBean.SeckillInfoBean.ListBean listBean = list.get(position);

        myViewHolder.setData(listBean);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        public ImageView ivFigure;
        public TextView tvCoverPrice;
        public TextView tvOriginPrice;
        public LinearLayout ll_root;

        public MyViewHolder(View itemView) {
            super(itemView);
            ivFigure = (ImageView) itemView.findViewById(R.id.iv_figure);
            tvCoverPrice = (TextView) itemView.findViewById(R.id.tv_cover_price);
            tvOriginPrice = (TextView) itemView.findViewById(R.id.tv_origin_price);
            ll_root = (LinearLayout) itemView.findViewById(R.id.ll_root);
            //秒杀点击事件(在adapter这里面一定不要忘记调用，这样在HomeFragmentAdapter那边才会生效)
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onSeckillRecyclerView!=null) {
                        onSeckillRecyclerView.onItemClick(getLayoutPosition());
                    }
                }
            });
        }

        public void setData(ResultBeanData.ResultBean.SeckillInfoBean.ListBean listBean) {
            tvCoverPrice.setText("￥" + listBean.getCover_price());
            tvOriginPrice.setText("￥" + listBean.getOrigin_price());
            Glide.with(mcontext)
                    .load(Constants.BASE_URl_IMAGE +listBean.getFigure())
                    .into(ivFigure);
//            ll_root.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    //  Toast.makeText(mContext, "position" + position, Toast.LENGTH_SHORT).show();
//                    if (onSeckillRecyclerView != null) {
//                        onSeckillRecyclerView.onClick(position);
//                    }
//                }
//            });
        }
    }
    /**
     * 秒杀点击事件的接口
     */
    public interface OnSeckillRecyclerView{
        /**
         * 当某条被点击的时候回调
         * @param position
         */
        void onItemClick(int position);
    }

    private OnSeckillRecyclerView onSeckillRecyclerView;

    /**
     * 秒杀点击事件
     * @param onSeckillRecyclerView
     */
    public void setOnSeckillRecyclerView(OnSeckillRecyclerView onSeckillRecyclerView) {
        this.onSeckillRecyclerView = onSeckillRecyclerView;
    }
}
